package fu.capstone.hanoicityguide.presentation.fragment.tourprovider;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.tourprovider.TourProviderBO;
import fu.capstone.hanoicityguide.business.bo.tourprovider.TourProviderBOImp;
import fu.capstone.hanoicityguide.business.model.Tour;
import fu.capstone.hanoicityguide.presentation.adapter.ListTourAdapter;
import fu.capstone.hanoicityguide.presentation.fragment.tour.DetailTourFragment;

@SuppressLint("ValidFragment")
public class DetailTourProviderFragment extends Fragment implements
		SearchView.OnQueryTextListener, SearchView.OnCloseListener {

	private TourProviderBO tourProviderBO;
	private int tourProviderId;
	private ListView tourProviderListView = null;
	private String title;
	private ListTourAdapter adapter = null;
	private ArrayList<Tour> arrTour;
	private String keyword;
	private String sorter;
	private SearchView searchView;

	public DetailTourProviderFragment() {

	}

	public DetailTourProviderFragment(int tourProviderId, String title) {
		this.tourProviderId = tourProviderId;
		this.title = title;
	}

	public int getTourProviderId() {
		return this.tourProviderId;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getSorter() {
		return sorter;
	}

	public void setSorter(String sorter) {
		this.sorter = sorter;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		try {
			setHasOptionsMenu(true);
		} catch (Exception e) {
			Log.e("DetailItineraryFragment", e.toString());
		}
		// list attraction by filter and sorter
		View browseTourProviderFragment = inflater.inflate(
				R.layout.fragment_list_tour, container, false);

		tourProviderBO = new TourProviderBOImp(container.getContext());

		// get data from database
		setKeyword("");
		setSorter(getResources().getStringArray(R.array.sorter_items)[0]);
		arrTour = new ArrayList<Tour>();
		arrTour = tourProviderBO.browseTourProvider(getTourProviderId(),
				getKeyword(), getSorter());

		// fill data into components
		tourProviderListView = (ListView) browseTourProviderFragment
				.findViewById(R.id.listTour);

		adapter = new ListTourAdapter(getActivity(),
				R.layout.adapter_list_tour, arrTour);

		tourProviderListView.setAdapter(adapter);

		tourProviderListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// // call Detail Fragment of Place
				Fragment browseTour = new DetailTourFragment(arrTour.get(
						position).getId());

				if (browseTour != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, browseTour)
							.commit();
					getActivity().setTitle(arrTour.get(position).getName());
				}
			}
		});

		return browseTourProviderFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.detail_tourprovider_menu, menu);
		// Associate searchable configuration with the SearchView
		searchView = (SearchView) menu.findItem(R.id.action_search_tour)
				.getActionView();
		setupSearchView();
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// create event for items of option menu in actionbar
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		AlertDialog dialog;

		switch (item.getItemId()) {
		case R.id.action_sort_tour: {
			builder.setTitle("Sort");
			builder.setItems(getResources()
					.getStringArray(R.array.sorter_items),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int item) {
							// sort by sorter
							setSorter(getResources().getStringArray(
									R.array.sorter_items)[item]);

							arrTour = tourProviderBO.browseTourProvider(
									getTourProviderId(), getKeyword(),
									getSorter());

							// reset array hotel in adapter
							adapter.setArr(arrTour);
							adapter.notifyDataSetChanged();
						}
					});

			dialog = builder.create();
			dialog.show();
		}
			break;
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void setupSearchView() {

		searchView.setIconifiedByDefault(false);
		searchView.setQueryHint("Tour Provider Name");

		searchView
				.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub
						if (hasFocus) {
							// drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
						} else {
							onClose();
						}
					}
				});

		searchView.setOnQueryTextListener(this);
		searchView.setOnCloseListener(this);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getActivity().getActionBar().setTitle(getTitle());
	}

	public boolean onQueryTextSubmit(String query) {
		setKeyword(query);
		if (getKeyword() != null && getSorter() != null) {
			arrTour = tourProviderBO.browseTourProvider(getTourProviderId(),
					getKeyword(), getSorter());
			adapter.setArr(arrTour);
			adapter.notifyDataSetChanged();
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		// TODO Auto-generated method stub
		setKeyword(newText);

		if (getKeyword() != null && getSorter() != null) {
			arrTour = tourProviderBO.browseTourProvider(getTourProviderId(),
					getKeyword(), getSorter());
			adapter.setArr(arrTour);
			adapter.notifyDataSetChanged();
			return true;
		} else {
			return false;
		}

	}

	@Override
	public boolean onClose() {
		// onclose search view
		// drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
		searchView.setQuery("", true);
		setKeyword("");
		arrTour = tourProviderBO.browseTourProvider(getTourProviderId(),
				getKeyword(), getSorter());
		adapter.setArr(arrTour);
		adapter.notifyDataSetChanged();
		return true;
	}
}
